package com.clear.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.clear.subject.common.enums.CategoryTypeEnum;
import com.clear.subject.common.enums.IsDeletedFlagEnum;
import com.clear.subject.domain.convert.SubjectLabelConverter;
import com.clear.subject.domain.entity.SubjectLabelBO;
import com.clear.subject.domain.service.SubjectLabelDomainService;
import com.clear.subject.infra.basic.entity.SubjectCategory;
import com.clear.subject.infra.basic.entity.SubjectLabel;
import com.clear.subject.infra.basic.entity.SubjectMapping;
import com.clear.subject.infra.basic.service.SubjectCategoryService;
import com.clear.subject.infra.basic.service.SubjectLabelService;
import com.clear.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectCategoryService subjectCategoryService;

    /**
     * 新增标签
     *
     * @param subjectLabelBO
     */
    @Override
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.add.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETE.getCode());

        int count = subjectLabelService.insert(subjectLabel);
        return count > 0;
    }

    /**
     * 更新标签
     *
     * @param subjectLabelBO
     */
    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.update.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);

        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    /**
     * 删除标签
     * 逻辑删除
     *
     * @param subjectLabelBO
     */
    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.delete.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        // 逻辑删除
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());

        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    /**
     * 查询标签（根据分类查询标签）
     *
     * @param subjectLabelBO
     * @return
     */
    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.queryLabelByCategoryId.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        Long categoryId = subjectLabelBO.getCategoryId();
        // =========如何当前分类是一级分类，则查询所有标签===========
        SubjectCategory subjectCategory = subjectCategoryService.queryById(categoryId);
        if (CategoryTypeEnum.PRIMARY.getCode() == subjectCategory.getCategoryType()) {
            SubjectLabel subjectLabel = new SubjectLabel();
            subjectLabel.setCategoryId(categoryId);
            subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETE.getCode());
            List<SubjectLabel> subjectLabelList = subjectLabelService.queryByCondition(subjectLabel);

            List<SubjectLabelBO> subjectLabelBOList = SubjectLabelConverter.INSTANCE.convertLabelTOBOList(subjectLabelList);
            return subjectLabelBOList;
        }


//        SubjectMapping subjectMapping = new SubjectMapping();
//        subjectMapping.setCategoryId(categoryId);
//        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETE.getCode());
//
//        // 根据categoryId去subject_mapping表查所有的label_id
//        List<SubjectMapping> subjectMappingList = subjectMappingService.queryLabelId(subjectMapping);
//        // 如果查找不到，则置空处理
//        if (CollectionUtils.isEmpty(subjectMappingList)) {
//            return Collections.emptyList();
//        }
//        // stream流获取所有的label_id
//        List<Long> labelIdList = subjectMappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
//        // 根据label_id去subject_label表查标签
//        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);
//        // PO => BO
//        List<SubjectLabelBO> boList = new LinkedList<>();
//        labelList.forEach(label -> {
//            SubjectLabelBO bo = new SubjectLabelBO();
//            bo.setId(label.getId());
//            bo.setLabelName(label.getLabelName());
//            bo.setSortNum(label.getSortNum());
//            bo.setCategoryId(categoryId);
//
//            boList.add(bo);
//        });
//        return boList;

        // todo 改为 直接根据categoryId去subject_label表查标签
        SubjectLabel subjectLabel = new SubjectLabel();
        subjectLabel.setCategoryId(categoryId);
        List<SubjectLabel> subjectLabelList = subjectLabelService.queryByCondition(subjectLabel);
        List<Long> labelIdList = subjectLabelList.stream().map(SubjectLabel::getId).collect(Collectors.toList());
        // 根据label_id去subject_label表查标签
        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);
        // PO => BO
        List<SubjectLabelBO> boList = new LinkedList<>();
        labelList.forEach(label -> {
            SubjectLabelBO bo = new SubjectLabelBO();
            bo.setId(label.getId());
            bo.setLabelName(label.getLabelName());
            bo.setSortNum(label.getSortNum());
            bo.setCategoryId(categoryId);

            boList.add(bo);
        });
        return boList;
    }
}
